package seabattle.io;

import seabattle.field.BattleField;
import seabattle.field.Cell;

public class Reflector {
	private Printer printer;

	public Reflector(Printer printer) {
		this.printer = printer;
	}

	public void drawFields(BattleField player, BattleField computer) {
		printer.print(fieldsToString(player, computer));
	}

	private String fieldsToString(BattleField player, BattleField computer) {
		final String rowLetter = "abcdefghij";
		final String columnLine = "  0  1  2  3  4  5  6  7  8  9   "
				+ "      0  1  2  3  4  5  6  7  8  9\n";
		StringBuilder field = new StringBuilder(columnLine);
		for (int row = 0; row < BattleField.SIZE; row++) {
			char rowName = rowLetter.charAt(row);
			field.append(rowName);
			for (Cell column : player.getColumn(row)) {
				field.append(createCell(column, player.getVisible()));
			}
			field.append(rowName).append("     ").append(rowName);
			for (Cell column : computer.getColumn(row)) {
				field.append(createCell(column, computer.getVisible()));
			}
			field.append(rowName).append("\n");
		}
		field.append(columnLine);
		return field.toString();
	}

	private String createCell(Cell cell, boolean showShips) {
		switch (cell) {
		case EMPTY:
			return " . ";
		case SHIP:
			if (showShips) {
				return "[ ]";
			} else {
				return " . ";
			}
		case SHOT:
			return " * ";
		case HIT:
			return "[*]";
		default:
			return "!";
		}
	}

}
